def f (x):
    for i in range(2, x):
      if x%i == 0:
          return (0)
    return (x)
print (" Введите четырёхзначное число")
k = int (input ())
if k < 1000 or k > 9999:
    print ('0')
else:
     a = k510
     d = (k//10) 410
     c = (k//100) 410
     b = k//1000
     
     
    mas=(0]*10
    mas [0]=a
    mas[1]=d 
    mas [2]=c
    mas [3]=b
    mas[4]=b*10+c
    mas[5]=c*10+d
    mas[6]-d*10+a
    mas[7]=b*100+c*10+d
    mas [8]=c*100+d*10+a
    mas[9]=k
    
     
    maxi=0
    for i in mas:
         b = f(i) 
         if b!=0 and b>maxi:
             maxi=b
print (maxi)

